'use strict';

const fs = require('fs');
const pump = require('mz-modules/pump');

const BaseController = require('./base.js');

class FocusController extends BaseController {

    async index() {
        // 获取轮播图的数据
        const result = await this.ctx.model.Focus.find({});
        await this.ctx.render('admin/focus/index', {
            list: result
        });
    }

    async add() {
        await this.ctx.render('admin/focus/add');
    }

    async doAdd() {
        // 获取上传的内容
        let parts = this.ctx.multipart({autoFields: true});
        let files = {};
        let stream;
        while ((stream = await parts()) != null ) {
            if (!stream.filename) {
                break;
            }
            let fieldname = stream.fieldname;   // file表单的 名字 face  

            let dir = await this.service.tools.getUploadFile(stream.filename);   // 返回上传路径地址与数据库保存地址
            let target = dir.uploadDir;
            let writeStream = fs.createWriteStream(target);

            await pump(stream, writeStream);  // 写入并销毁当前流  egg-demo方法

            files = Object.assign(files, {
                [fieldname]: dir.saveDir
            })
        }

        let focus = new this.ctx.model.Focus(Object.assign(files, parts.field));
        let result = await focus.save();

        await this.success('/admin/focus', '增加轮播图成功');

    }

    async edit() {
        let { ctx } = this;
        let id = ctx.request.query.id;
        let result = await ctx.model.Focus.find({"_id": id});

        await this.ctx.render('admin/focus/edit', {
            list: result[0]
        });
    }

    async doEdit() {
        // 获取上传的内容
        let parts = this.ctx.multipart({autoFields: true});
        let files = {};
        let stream;
        while ((stream = await parts()) != null ) {
            if (!stream.filename) {
                break;
            }
            let fieldname = stream.fieldname;   // file表单的 名字 face  
            // 上传图片的目录
            let dir = await this.service.tools.getUploadFile(stream.filename);   // 返回上传路径地址与数据库保存地址
            let target = dir.uploadDir;
            let writeStream = fs.createWriteStream(target);

            await pump(stream, writeStream);  // 写入并销毁当前流  egg-demo方法

            files = Object.assign(files, {
                [fieldname]: dir.saveDir
            })
        }
        
        // 修改操作
        let id = parts.field.id;
        var updateResult = Object.assign(files, parts.field);
        console.log(updateResult)

        let result = await this.ctx.model.Focus.updateOne({"_id":id}, updateResult)

        await this.success('/admin/focus', '修改轮播图成功');
    }


    /*  只支持上传一个文件
    async doSingleUpload() {

        // 单文件上传
        const stream = await this.ctx.getFileStream();   // 获取表单提交的数据

        // console.log(stream)
        var fields = stream.fields;   // 表单的其他数据

        // 上传的目录 目录要存在
        const target = 'app/public/admin/upload/' + path.basename(stream.filename);

        const writeStream = fs.createWriteStream(target);

        // stream.pipe(writeStream);    // 文档写法: 读取流 - 管道 - 写入流    注意: 流必须消费掉， 否则浏览器会卡死
        await pump(stream, writeStream);

        this.ctx.body = {
            url: target,
            fields: stream.fields
        }
    }
    
    async doMultiUpload() {

        const parts = this.ctx.multipart({autoFields: true});

        const files = [];

        let stream;
        while ((stream = await parts()) != null ) {
            if (!stream.filename) {
                return;
            }
            const filename = stream.filename.toLowerCase();
            const fieldname = stream.fieldname;
            const target = 'app/public/admin/upload/' + path.basename(filename);
            const writeStream = fs.createWriteStream(target);
            await pump(stream, writeStream);  // 写入并销毁当前流
            files.push({
                [fieldname]: target
            });
        }

        this.ctx.body = {
            files: files,
            fields: parts.field
        }
    }
    */
}

module.exports = FocusController;
